import 'package:flutter/material.dart';
import '../router/app_router.dart';

// 以后页面跳转就可以这样写。 RouterUtils.toRouter(context, RouterValues.你定义的常量名);
// 或者直接跳页面的形式  RouterUtils.toPage(context, PlayVideoPage());

class RouterUtils{

  static bool canPop =  true;

  static dynamic arguments;

  static getArgument(){
    var data = arguments;
    arguments = null;
    return data;
  }

  //路由跳转
  static Future<dynamic> toRouter(BuildContext context, String router, {dynamic arguments, bool isLogin = false})async{
    RouterUtils.arguments = arguments;
    return await Navigator.pushNamed(context, router, arguments: arguments);
  }

  //路由替换
  static Future<dynamic> replaceRouter(BuildContext context,String router,{dynamic arguments,bool isLogin = false})async{
    RouterUtils.arguments = arguments;
    return await Navigator.pushReplacementNamed(context, router,arguments: arguments);
  }

  static Future<dynamic> backToRouter(BuildContext context,String router,{dynamic arguments,bool isLogin = false})async{
    RouterUtils.arguments = arguments;
    return await Navigator.popAndPushNamed(context, router,arguments: arguments);
  }

  static Future<dynamic> toPage(BuildContext context,Widget page,{bool isLogin = false})async{
    return await Navigator.push(context, MaterialPageRoute(builder: (context) => page));
  }

  static Future toRouterCloseAll(BuildContext context,String router,{dynamic arguments})async{
    RouterUtils.arguments = arguments;
    return await Navigator.pushNamedAndRemoveUntil(context,router, ModalRoute.withName(router),arguments: arguments);
  }

  //返回上一页
  static back(BuildContext context,{dynamic arguments}){
    if(canPop) {
      Navigator.pop(context, arguments);
    }else{
      toRouterCloseAll(context, RouterValues.tabPage);
    }
  }

}